package org.lds.message.center.plugin.core.autoconfigure;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.lds.message.center.common.model.request.GetTemplateAdminRequest;
import org.lds.message.center.plugin.core.constant.PluginCoreConstant;
import org.lds.message.center.plugin.core.kafka.ClearTemplateCacheConsumer;
import org.lds.message.center.plugin.core.remote.RemoteService;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class MessageCenterCacheAutoConfiguration {
    @Bean
    public CacheManager messageCenterCacheConfig() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setAllowNullValues(false);
        cacheManager.setCacheNames(Arrays.asList(PluginCoreConstant.CACHE_NAME));
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .initialCapacity(10)
                .expireAfterWrite(1, TimeUnit.DAYS) // 设置写入后60秒过期
                .maximumSize(100)); // 设置最大缓存项数为100
        return cacheManager;
    }

    @Bean
    public ClearTemplateCacheConsumer clearTemplateCacheConsumer(RemoteService remoteService) {
        return new ClearTemplateCacheConsumer(remoteService, GetTemplateAdminRequest.class);
    }

}
